/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.session;

import com.thienloc.omap.entity.Kqxs;
import java.util.Date;
import java.util.List;
import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
 *
 * @author GiangLT
 */
@Stateless
public class KqxsFacade extends AbstractFacade<Kqxs> {

    @PersistenceContext(unitName = "omapPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public KqxsFacade() {
        super(Kqxs.class);
    }

    public Kqxs find(String tinhQuay, String ngayQuayStr) throws Exception {
        try {
            TypedQuery<Kqxs> q = em.createNamedQuery("Kqxs.findByTinhAndNgay", Kqxs.class);
            q.setParameter("tinhQuay", tinhQuay);
            q.setParameter("ngayQuayStr", ngayQuayStr);
            Kqxs result = q.getSingleResult();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(this.getClass()).error("", ex);
            return null;
        }
    }

    public List<Kqxs> find(String tinhQuay, Date startDate, Date endDate) throws Exception {
        try {
            TypedQuery<Kqxs> q = em.createNamedQuery("Kqxs.findByTinhBetweenDate", Kqxs.class);
            q.setParameter("tinhQuay", tinhQuay);
            q.setParameter("startDate", startDate);
            q.setParameter("endDate", endDate);
            List<Kqxs> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(this.getClass()).error("", ex);
            return null;
        }
    }

    public List<Kqxs> findByArea(String phamVi, Date startDate, Date endDate) throws Exception {
        try {
            TypedQuery<Kqxs> q = em.createNamedQuery("Kqxs.findByPhamViBetweenDate", Kqxs.class);
            q.setParameter("phamVi", phamVi);
            q.setParameter("startDate", startDate);
            q.setParameter("endDate", endDate);
            List<Kqxs> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(this.getClass()).error("", ex);
            return null;
        }
    }

    public List<Kqxs> findBetweenDate(Date startDate, Date endDate) throws Exception {
        try {
            TypedQuery<Kqxs> q = em.createNamedQuery("Kqxs.findBetweenDate", Kqxs.class);
            q.setParameter("startDate", startDate);
            q.setParameter("endDate", endDate);
            List<Kqxs> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {

            LoggerFactory.getLogger(this.getClass().getName()).error("", ex);
            return null;
        }
    }

    public List<String> findAvaiableProvinces() {
        try {
            Query q = em.createQuery("SELECT distinct k.tinhQuay FROM Kqxs k");
            List<String> result = (List<String>) q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(this.getClass()).error("", ex);
            return null;
        }
    }

    public List<String> findAvaiableAreas() {
        try {
            Query q = em.createQuery("SELECT distinct k.phamVi FROM Kqxs k");
            List<String> result = (List<String>) q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(this.getClass()).error("", ex);
            return null;
        }
    }

    public void deleteById(Long id) {
        try {
            Query q = em.createNamedQuery("Kqxs.deleteById");
            q.setParameter("id", id);
            q.executeUpdate();
        } catch (Exception ex) {
            LoggerFactory.getLogger(this.getClass()).error("", ex);
        }
    }

    public void fixKqxs() throws Exception {
        try {
            Query q = em.createNativeQuery("{call UPDATE_XS}");
            q.executeUpdate();
        } catch (Exception ex) {
            LoggerFactory.getLogger(this.getClass()).error("", ex);
            throw ex;
        }
    }
}
